//package com.example.moude_base.controller
//
//import android.animation.ValueAnimator
//import android.annotation.SuppressLint
//import android.app.Activity
//import android.app.Dialog
//import android.content.Context
//import android.os.Handler
//import android.os.Looper
//import android.view.LayoutInflater
//import android.view.Window
//import android.view.animation.Animation
//import android.view.animation.LinearInterpolator
//import android.view.animation.RotateAnimation
//
//@SuppressLint("StaticFieldLeak")
//object LoadingController {
//    private var loadView: CenterLoadingView? = null
//    private var count = 0
//    private var activity: Activity? = null
//    private const val MIN_SHOW_TIME = 1000
//    private const val MIN_DELAY = 400
//    private val mHandler = Handler(Looper.getMainLooper())
//    private var mPostedHide = false
//    private var mPostedShow = false
//    private var mDismissed = false
//    private var mStartTime: Long = -1 // 开始显示时的时间
//    private var isCanceledByUser = false
//
//    fun setCancelable(cancelable: Boolean) {
//        loadView?.setCancelable(cancelable)
//    }
//    fun isShowing(): Boolean {
//        return loadView?.isShowing == true
//    }
//    fun setActivity(activity: Activity?) {
//        this.activity = activity
//    }
//
//    private val mDelayedHide = Runnable {
//        mPostedHide = false
//        mStartTime = -1
//        loadView?.dismiss()
//    }
//
//
//    private val mDelayedShow = Runnable {
//        mPostedShow = false
//        if (!mDismissed && activity != null && !activity!!.isFinishing && !activity!!.isDestroyed) {
//            mStartTime = System.currentTimeMillis()
//            try {
//                if (!loadView!!.isShowing) {
//                    loadView?.show()
//                }
//            } catch (e: Exception) {
//                e.printStackTrace()
//            }
//        }
//    }
//
//
//    private val onAttachedCallback={
//        mHandler.removeCallbacks(mDelayedShow)
//    }
//    private val onDetachedCallback={
//        mHandler.removeCallbacks(mDelayedHide)
//    }
//    fun showLoading() {
//        activity?.let {
//            if (it.isFinishing || it.isDestroyed) {
//                return
//            }
//            loadView?.let { loadView ->
//                if (loadView.isShowing) {
//                    loadView.dismiss()
//                }
//            }
////            if (loadView == null) {
//                loadView = CenterLoadingView(it, onAttachedCallback, onDetachedCallback).apply {
//                    setOnCancelListener {
//                        isCanceledByUser = true
//                    }
//                    setOnDismissListener {
//                        if (isCanceledByUser) {
//                            count--
//                        }
//                        isCanceledByUser = false
//                    }
//                }
////            }
//            mStartTime = -1
//            mDismissed = false
//            mHandler.removeCallbacks(mDelayedHide)
//            mPostedHide = false
//            if (!mPostedShow) {
//                mHandler.postDelayed(mDelayedShow, MIN_DELAY.toLong())
//                mPostedShow = true
//            }
//            if (count <= 0) {
//                count = 0
//            }
//            count++
//        }
//    }
//
//    fun dismissLoading(clazz: Class<Activity>? =null) {
//        if(clazz != null && activity?.javaClass != clazz) return
//        count--
//        if (count <= 0) {
//            clearLoading()
//        }
//    }
//
//    fun clearLoading() {
//        mDismissed = true
//        mHandler.removeCallbacks(mDelayedShow)
//        mPostedShow = false
//        val diff = System.currentTimeMillis() - mStartTime
//        if (diff >= MIN_SHOW_TIME || mStartTime == -1L || activity == null || activity!!.isFinishing || activity!!.isDestroyed) {
//            loadView?.dismiss()
//        } else {
//            if (!mPostedHide) {
//                mHandler.postDelayed(mDelayedHide, MIN_SHOW_TIME - diff)
//                mPostedHide = true
//            }
//        }
//        count = 0
//    }
//
//    class CenterLoadingView(context: Context,val onAttachedCallback:()->Unit,val onDetachedCallback:()->Unit) : Dialog(context, R.style.loading_dialog) {
//        private var mBinding: DfrdNetDataViewLoadingBinding
//        private var animation: Animation? = null
//        override fun onAttachedToWindow() {
//            super.onAttachedToWindow()
//            onAttachedCallback()
//        }
//
//        override fun onDetachedFromWindow() {
//            super.onDetachedFromWindow()
//            onDetachedCallback()
//        }
//        init {
//            requestWindowFeature(Window.FEATURE_NO_TITLE)
//            mBinding = DfrdNetDataViewLoadingBinding.inflate(LayoutInflater.from(context))
//            setContentView(mBinding.root)
//            setCanceledOnTouchOutside(false)
//            initAnim()
//        }
//
//        private fun initAnim() {
//            animation = RotateAnimation(
//                360f,
//                0f,
//                Animation.RELATIVE_TO_SELF,
//                0.5f,
//                Animation.RELATIVE_TO_SELF,
//                0.5f
//            )
//            animation?.duration = 1000
//            animation?.repeatCount = ValueAnimator.INFINITE
//            animation?.repeatMode = ValueAnimator.RESTART
//            animation?.interpolator = LinearInterpolator()
//        }
//
//        override fun show() {
//            super.show()
//            mBinding.iv.startAnimation(animation)
//        }
//
//        override fun dismiss() {
//            try {
//                super.dismiss()
//                mBinding.iv.clearAnimation()
//            }catch (e:Exception){
//                TLog.e(e)
//            }
//        }
//
//    }
//}
